package rj.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CountServlet", value = "/CountServlet")
public class CountServlet extends HttpServlet {

    public  void  init() {
        int  count  =  0;  //  给次数设置一个初始值  0
        //  将  0  存入到  ServletContext中
        getServletContext().setAttribute("count",  count);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  将原来的次数count从ServletContext中取出来
        int  count  =  (int)  getServletContext().getAttribute("count");
        count  +=  1;
        //  存入到ServletContext中
        getServletContext().setAttribute("count",  count);
        //  向页面输出欢迎光临!
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>欢迎光临本网站!  这是您第"  +  count  +  "次访问</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,  response);
    }
}
